package com.yyysj.friend.controller;

import com.yyysj.common.core.constants.HttpConstants;
import com.yyysj.common.core.domain.LoginUserVO;
import com.yyysj.common.core.domain.Result;
import com.yyysj.friend.domain.dto.UserDTO;
import com.yyysj.friend.domain.dto.UserUpdateDTO;
import com.yyysj.friend.domain.vo.UserVO;
import com.yyysj.friend.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user")
@RestController
@RequiredArgsConstructor
@Tag(name = "用户模块")
public class UserController {

    private final IUserService userService;

    @Operation(summary = "发送验证码", description = "发送验证码")
    @PostMapping("/sendCode")
    public Result<?> sendCode(@RequestBody UserDTO userDTO) {
        return Result.build(userService.sendCode(userDTO));
    }

    @Operation(summary = "验证码登录",  description = "手机号和验证码登录")
    @PostMapping("/code/login")
    public Result<String> codeLogin(@RequestBody UserDTO userDTO) {
        String token = userService.codeLogin(userDTO.getPhone(), userDTO.getCode());
        if(token == null) {
            return Result.fail();
        } else {
            return Result.ok(token);
        }
    }

    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return Result.build(userService.logout(token));
    }

    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        LoginUserVO loginUserVO = userService.info(token);
        if (loginUserVO == null) {
            return Result.fail();
        } else {
            return Result.ok(loginUserVO);
        }
    }

    @GetMapping("/detail")
    public Result<UserVO> detail() {
        return Result.ok(userService.detail());
    }

    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO) {
        return Result.build(userService.edit(userUpdateDTO));
    }

    @PutMapping("/head-image/update")
    public Result<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO) {
        return Result.build(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
    }
}
